package com.qc31.monitorlib.ui.mile

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.qc31.baselib.base.BaseToolbarRecycleActivity
import com.qc31.baselib.utils.ToolbarHelper
import com.qc31.comlib.Keys
import com.qc31.comlib.entity.DayMile
import com.qc31.monitorlib.databinding.HeadStopMileBinding
import com.qc31.monitorlib.ui.adapter.DayMileAdapter

/** @Author Jasper @Time 2020/12/8 @Desc 里程查询结果 */
class MileInfoActivity : BaseToolbarRecycleActivity() {
    private lateinit var time: String
    private lateinit var long: String
    private lateinit var mile: String
    private lateinit var infoEntity: MutableList<DayMile.MileInfo>

    override fun initView() {
        val entity = intent.getParcelableExtra<DayMile>(Keys.INTENT_DATA)
        val title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        time = intent.getStringExtra(Keys.INTENT_TIME) ?: ""
        ToolbarHelper.setToolbar(this, title)

        entity?.let {
            long = it.long
            mile = it.mile
            infoEntity = it.list
        }
        super.initView()
    }

    override fun initRecyclerView() {
        val dayMileAdapter = DayMileAdapter()
        mRecycler.adapter = dayMileAdapter
        dayMileAdapter.setList(infoEntity)
        dayMileAdapter.addHeaderView(initHeadView())
    }

    override fun initHeadView(): View {
        val binding = HeadStopMileBinding.inflate(layoutInflater)
        binding.lrTvInfoTime.setRightText(time)
        binding.lrTvInfoLong.setRightText("$long/$mile km")
        return binding.root
    }

    override fun setListener() {
        binding.tvToTop.setOnClickListener {
            mRecycler.smoothScrollToPosition(0)
        }

        mRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val position = mLayoutManger.findFirstVisibleItemPosition()
                if (position > 0) {
                    binding.tvToTop.visibility = View.VISIBLE
                } else {
                    binding.tvToTop.visibility = View.GONE
                }
            }
        })
    }

}